package cn.tedu.yxcar.product.controller;

import cn.tedu.yxcar.product.pojo.vo.ArticleSearchVO;
import cn.tedu.yxcar.product.pojo.vo.CarListVO;
import cn.tedu.yxcar.product.pojo.vo.CarShowVO;
import cn.tedu.yxcar.product.service.ICarService;
import cn.tedu.yxcar.product.web.JsonPage;
import cn.tedu.yxcar.product.web.JsonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.spring.web.json.Json;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/cars")
@Api(tags = "汽车管理模块")
@Validated
public class CarController {

    @Autowired
    private ICarService carService;

    @GetMapping("/select")
    @ApiOperation("搜索汽车列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name = "page",example = "1"),
            @ApiImplicitParam(value = "每页条数",name = "pageSize",example = "10"),
            @ApiImplicitParam(value = "关键词",name = "keywords",example = "20")
    })
    public JsonResult<JsonPage<CarListVO>> listSeckillSpus(
            @Param("page") Integer page, @Param("pageSize") Integer pageSize, @Param("keywords") String keywords){
        PageHelper.startPage(page,pageSize);
        List<CarListVO> list=
                carService.carSearch(keywords);
        JsonPage<CarListVO> jsonPage = JsonPage.restPage(PageInfo.of(list));
        return JsonResult.ok(jsonPage);
    }
    @GetMapping("/show")
    @ApiOperation("展示汽车列表")
    public List<CarShowVO> getAll(){
        return carService.carShow();
    }

    @GetMapping("/showKey")
    @ApiOperation("根据关键词展示汽车列表")
    @ApiImplicitParam(value = "关键词",name = "key",example = "1")
    public JsonResult showKey(
            @Param("key") String key){
        List<CarListVO> list = carService.carSearch(key);
        System.out.println(list);
        return JsonResult.ok(list);
    }

    @GetMapping("/{model}")
    @ApiOperation("根据汽车名称展示汽车列表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "model", value = "车型号", required = true, dataType = "String")
    })
    public List<CarShowVO> showByModel(String model){
        List<CarShowVO> carShowVO = carService.carShowByModel(model);
        return carShowVO;

    }
}
